package com.jdrx.ocp.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jdrx.ocp.beans.constans.EOnline;
import com.jdrx.ocp.beans.dto.userLogin.QueryUserLoginLogsDTO;
import com.jdrx.ocp.beans.entity.UserLoginLogsPO;
import com.jdrx.ocp.beans.vo.UserLoginLogsVO;
import com.jdrx.ocp.dao.UserLoginLogsDAO;
import com.jdrx.platform.commons.rest.beans.dto.PageBaseDTO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.jdbc.beans.vo.PageVO;
import com.jdrx.sw.sewage.service.common.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 登录日志记录
 *
 * @Author: Huangxg
 * @Date: 2018/11/20 13:36
 */
@Service
public class UserLoginLogsService {

    private static final Logger logger = LoggerFactory.getLogger(UserLoginLogsService.class);

    @Autowired
    UserLoginLogsDAO userLoginLogsDAO;
    @Autowired
    RedisService redisService;
    @Value("${jdrx.redis.ocp.user.state.online}")
    String userOnlineStateKey;

    public void insert(UserLoginLogsPO po) throws BizException {
        try{
            userLoginLogsDAO.insert(po);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            throw new BizException("服务异常！");
        }
    }

    /**
     * 获取当前用户的登录日志信息
     * @param dto
     * @param uid
     * @return
     */
    public PageVO<UserLoginLogsVO> queryUserListByPage(PageBaseDTO dto,Integer uid){
        Page<UserLoginLogsVO> page = PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        userLoginLogsDAO.queryUserList(uid);
        PageVO<UserLoginLogsVO> pageVO = new PageVO<UserLoginLogsVO>(page);
        pageVO.setData(onlineState(page.getResult()));
        return pageVO;
    }

    public PageVO<UserLoginLogsVO> queryListByPage(QueryUserLoginLogsDTO queryUserLoginLogsDTO) throws BizException {
        try {
            Map<String, Object> queryMap = new HashMap<>();
            queryMap.put("name",queryUserLoginLogsDTO.getName());
            queryMap.put("realName",queryUserLoginLogsDTO.getName());
            Page<UserLoginLogsVO> page = PageHelper.startPage(queryUserLoginLogsDTO.getPageNum(), queryUserLoginLogsDTO.getPageSize());
            userLoginLogsDAO.queryList(queryMap);
            PageVO<UserLoginLogsVO> pageVO = new PageVO<UserLoginLogsVO>(page);
            pageVO.setData(onlineState(page.getResult()));
            return pageVO;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BizException(e.getMessage());
        }
    }

    private List<UserLoginLogsVO> onlineState(List<UserLoginLogsVO> list){
        for (UserLoginLogsVO logsVO : list){
            logsVO.setOnlineSate(EOnline.NO.getValue());
            if(redisService.existsKey(userOnlineStateKey + logsVO.getDevice() + ":" + logsVO.getUserId())) {
                logsVO.setOnlineSate(EOnline.YES.getValue());
            }
        }
        return list;
    }

    /**
     * 获取用户登录以及在线状态
     * @param queryUserLoginLogsDTO
     * @return
     * @throws BizException
     */
    public List<UserLoginLogsVO> queryUserLogin(QueryUserLoginLogsDTO queryUserLoginLogsDTO) throws BizException {
        try {
            Map<String, Object> queryMap = new HashMap<>(4);
            queryMap.put("name",queryUserLoginLogsDTO.getName());
            queryMap.put("realName",queryUserLoginLogsDTO.getName());
            List<UserLoginLogsVO> list = userLoginLogsDAO.queryAllUserLoginLogs(queryMap);
            //获取在线状态
            onlineState(list);
            list.sort(Comparator.comparing(UserLoginLogsVO::getOnlineSate)
                    .thenComparing(UserLoginLogsVO::getLoginTime,Comparator.reverseOrder())
            );
            return list;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BizException(e.getMessage());
        }
    }

}
